import { getToken } from '@/utils/storage';

const initialState = {
  auth: getToken(),
  profile: {} as ProfileType,
  userInfo: {} as UserInfoType,
};
//个人中心用户信息类型
type UserInfoType = {
  id: string;
  photo: string;
  name: string;
  is_media: string;
  intro: string;
  certi: string;
  art_count: string;
  follow_count: string;
  fans_count: string;
  like_count: string;
};
export type Auth = {
  token: string;
  refresh_token: string;
};
//个人资料类型
type ProfileType = {
  id: string;
  name: string;
  photo: string;
  mobile: string;
  gender: number;
  birthday: string;
  intro: string | null;
};
//编辑个人资料类型
export type EditProfileType = {
  name?: string;
  gender?: number;
  birthday?: string;
  real_name?: string;
  intro?: string;
};
export type UserAction =
  | { type: 'user/login'; payload: Auth }
  | { type: 'user/userInfo'; payload: UserInfoType }
  | { type: 'user/profile'; payload: ProfileType }
  | { type: 'user/photo'; payload: string }
  | { type: 'user/clearToken' };
export default function userReducer(
  state = initialState,
  action: UserAction
): typeof initialState {
  switch (action.type) {
    //登录
    case 'user/login':
      return {
        ...state,
        auth: action.payload,
      };
    case 'user/userInfo':
      return {
        ...state,
        userInfo: action.payload,
      };
    case 'user/profile':
      return {
        ...state,
        profile: action.payload,
      };
    case 'user/photo':
      return {
        ...state,
        profile: { ...state.profile, photo: action.payload },
      };
    case 'user/clearToken':
      return {
        ...state,
        auth: getToken(),
      };

    default:
      return state;
  }
}
